<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
require_once APP . '/application/models/SfdataModel.php';
require_once APP . '/application/models/Util.php';

/**
 * 服务器列表管理
 * @package admin
 */
class ServerController extends AdminAction
{
    
    public function init()
    {
        // $this->_config = Registry::get ('config');
    }


    public function addAction()
    {
        //加载默认视图
    }
    
    public function doaddAction()
    {
    	$params = $this->getAllParams();
    	
    	// 时间处理
    	$params['sdatetime'] = $params['sdatetime_day'] . ' ' . $params['sdatetime_hour'] . ':' . $params['sdatetime_minute'];
    	$params['pubtime'] = time();
    	$params['pubip'] = Util::getip(true);
    	
    	$admininfo = AdminUser::getLoginInfo();
    	$params['adminid'] = $admininfo['id'];
    	
    	$model = new SfdataModel($this->db);
    	$model->add($params);
    	
    	$this->view->msg = '添加成功！';
    	$this->view->url = $this->url($this->view->baseUrl, "server", "add");
    	$this->showmessage();
    }
    
    public function listAction()
    {
    	$nowp = (int)$this->getParam("nowp", 1);
    	$per_page = 30;
    	$where = $this->getParam("where");
    	
    	$model = new SfdataModel($this->db);
    	$result = $model->fetchAll($nowp, $per_page, $where);
    	
    	$this->view->list = $result['list'];
    	$this->view->page = $result['page'];
    	$this->view->nowp = $nowp;
    	$this->view->where = $where;
    }
    
    public function editAction()
    {
    	$where = $this->getParam("where");
    	$nowp = (int)$this->getParam("nowp", 1);
    	$id = (int)$this->getParam("id");
    	
    	$model = new SfdataModel($this->db);
    	$info = $model->show($id);
    	
    	$this->view->info = $info;
    	$this->view->id = $id;
    	$this->view->where = $where;
    	$this->view->nowp = $nowp;
    }
    
    public function doeditAction()
    {
    	$where = $this->getParam("where");
    	$nowp = (int)$this->getParam("nowp", 1);
    	$id = (int)$this->getParam("id");
    	$params = $this->getAllParams();
    	
    	// 时间处理
    	$params['sdatetime'] = $params['sdatetime_day'] . ' ' . $params['sdatetime_hour'] . ':' . $params['sdatetime_minute'];
    	
    	$model = new SfdataModel($this->db);
    	$model->change($id, $params);
    	
    	$this->view->msg = '编辑成功！';
    	$this->view->url = $this->url($this->view->baseUrl, "server", "list", "where=" . urlencode($where) . '&nowp=' . $nowp);
    	$this->showmessage();
    }
    
    public function batchAction()
    {
    	$ids = $this->getParam("ids");
    	$opp = $this->getParam("type");
    	
    	if ($opp == 'del'){
    		$this->db->delete("sfdata", "where id in(" . implode(",", $ids) . ')');
    	}else{
    		if (in_array($opp, array("isbest", "ishot", "ischeck"))){
    			$this->db->update("sfdata", array($opp => 1),  "where id in(" . implode(",", $ids) . ')');
    		}
    	}
    	
    	FileCache::getInstance()->deleteTag("sfdata");
    }
}
